package com.fhz.mvvm.api

import androidx.lifecycle.LiveData
import com.fhz.loginmvvm.callback.ApiResponse
import com.fhz.loginmvvm.callback.LiveDataCallAdapterFactory
import com.fhz.loginmvvm.util.Cons
import com.fhz.mvvm.entity.User
import com.fhz.mvvm.entity.UserEntity
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.POST
import retrofit2.http.Query

/**
 * 时间:2023/3/28
 * @author Mr.Feng
 * 简述: 用依赖注入的方式提供接口
 */
interface YaoServiceApi {
    companion object {
        fun create(): YaoServiceApi {
            val logger = HttpLoggingInterceptor().apply { level = HttpLoggingInterceptor.Level.BASIC }

            val client = OkHttpClient.Builder()
                .addInterceptor(logger)
                .build()

            return Retrofit.Builder()
                .baseUrl(Cons.HOST)
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(LiveDataCallAdapterFactory())
                .build()
                .create(YaoServiceApi::class.java)
        }
    }

    @POST(Cons.LOGIN)
    //Callback ->
    fun login(@Query("username") username:String, @Query("password") password:String) : LiveData<ApiResponse<UserEntity>>
}